package com.thememaker.lockstyle;

import com.thememaker.lockstyle.bean.animation.*;
import javafx.animation.*;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Duration;

import java.nio.file.Paths;
import java.util.List;

/**
 * Created by binghui.hang on 2017/8/18.
 */
public class AnimationUtils {

    public static SequentialTransition initSourceAnimation(ImageView imageView, String lockscreenPathString, List<Source> pData, double rate){
        boolean canPlayOne = false;
        //创建时间轴
        SequentialTransition st = new SequentialTransition(imageView);

        for (int i = 0; i < pData.size(); i++) {
            Timeline timeline=new Timeline();
            Source source = pData.get(i);
            if (i==0){
                KeyValue kv=new KeyValue(imageView.imageProperty(), new Image(Paths.get(lockscreenPathString,source.getSrc()).toUri().toString()));
                KeyFrame kf=new KeyFrame(Duration.millis(0), kv);
                timeline.getKeyFrames().add(kf);
                st.getChildren().add(timeline);
                continue;
            }
            if (i==pData.size()-1){
                if ("100000000000000".equals(source.getTime())){
                    canPlayOne = true;
                    KeyValue kv=new KeyValue(imageView.imageProperty(), new Image(Paths.get(lockscreenPathString,source.getSrc()).toUri().toString()));
                    KeyFrame kf=new KeyFrame(Duration.millis(1), kv);
                    timeline.getKeyFrames().add(kf);
                    st.getChildren().add(timeline);
                    break;
                }
            }
            Source source1 = pData.get(i-1);
            KeyValue kv=new KeyValue(imageView.imageProperty(), new Image(Paths.get(lockscreenPathString,source.getSrc()).toUri().toString()));
            KeyFrame kf=new KeyFrame(Duration.millis((Double.parseDouble(source.getTime())-Double.parseDouble(source1.getTime()))), kv);
            timeline.getKeyFrames().add(kf);
            st.getChildren().add(timeline);

        }
        if (canPlayOne){
            st.setCycleCount(1);
        }else{
            st.setCycleCount(Timeline.INDEFINITE);
        }
        return st;
    }

    public static SequentialTransition initPositionAnimation(ImageView imageView, List<Position> pData, double rate){
        SequentialTransition st=new SequentialTransition();
        boolean canPlayOne = false;
        for (int i = 0; i < pData.size(); i++) {
            Position pt = pData.get(i);
            if (i==0){
                TranslateTransition t=new TranslateTransition(Duration.millis(0), imageView);
                t.setFromX(0);
                t.setFromY(0);
                t.setToX(Double.parseDouble(pt.getX())*rate);
                t.setToY(Double.parseDouble(pt.getY())*rate);
                st.getChildren().add(t);
                continue;
            }
            Position pt1 = pData.get(i-1);
            if (i == pData.size()-1){
                if ("100000000000000".equals(pt.getTime())){
                    canPlayOne = true;
                    TranslateTransition t = new TranslateTransition(Duration.millis(1), imageView);
                    t.setFromX(Double.parseDouble(pt1.getX())*rate);
                    t.setFromY(Double.parseDouble(pt1.getY())*rate);
                    t.setToX(Double.parseDouble(pt.getX())*rate);
                    t.setToY(Double.parseDouble(pt.getY())*rate);
                    st.getChildren().add(t);
                    break;
                }
            }
            TranslateTransition t = new TranslateTransition(Duration.millis(Double.parseDouble(pt.getTime())-Double.parseDouble(pt1.getTime())), imageView);
            t.setFromX(Double.parseDouble(pt1.getX())*rate);
            t.setFromY(Double.parseDouble(pt1.getY())*rate);
            t.setToX(Double.parseDouble(pt.getX())*rate);
            t.setToY(Double.parseDouble(pt.getY())*rate);
            st.getChildren().add(t);
        }
        if (canPlayOne){
            st.setCycleCount(1);
        }else{
            st.setCycleCount(Timeline.INDEFINITE);
        }
        return st;
    }


    public static SequentialTransition initSizeAnimation(ImageView imageView, List<Size> pData, double rate){
        SequentialTransition st=new SequentialTransition();
        boolean canPlayOne = false;
        for (int i = 0; i < pData.size(); i++) {
            Size pt = pData.get(i);
            if (i==0){
                ScaleTransition t=new ScaleTransition(Duration.millis(0), imageView);
                t.setToX(Double.parseDouble(pt.getW())/imageView.getFitWidth()*rate);
                t.setToY(Double.parseDouble(pt.getH())/imageView.getFitHeight()*rate);
                st.getChildren().add(t);
                continue;
            }
            Size pt1 = pData.get(i-1);
            if (i == pData.size()-1){
                if ("100000000000000".equals(pt.getTime())){
                    canPlayOne = true;
                    ScaleTransition t=new ScaleTransition(Duration.millis(1), imageView);
                    t.setToX(Double.parseDouble(pt.getW())/imageView.getFitWidth()*rate);
                    t.setToY(Double.parseDouble(pt.getH())/imageView.getFitHeight()*rate);
                    st.getChildren().add(t);
                    break;
                }
            }
            ScaleTransition t=new ScaleTransition(Duration.millis(Double.parseDouble(pt.getTime())-Double.parseDouble(pt1.getTime())), imageView);
            t.setToX(Double.parseDouble(pt.getW())/imageView.getFitWidth()*rate);
            t.setToY(Double.parseDouble(pt.getH())/imageView.getFitHeight()*rate);
            st.getChildren().add(t);
        }
        if (canPlayOne){
            st.setCycleCount(1);
        }else{
            st.setCycleCount(Timeline.INDEFINITE);
        }
        return st;
    }


    public static SequentialTransition initRotationAnimation(ImageView imageView, List<Rotation> pData, double rate){
        SequentialTransition st=new SequentialTransition();
        boolean canPlayOne = false;
        for (int i = 0; i < pData.size(); i++) {
            Timeline timeline=new Timeline();
            Rotation pt = pData.get(i);
            if (i==0){
                KeyValue kv=new KeyValue(imageView.rotateProperty(),Double.parseDouble(pt.getAngle()));
                KeyFrame kf=new KeyFrame(Duration.millis(0), kv);
                timeline.getKeyFrames().add(kf);
                st.getChildren().add(timeline);
                continue;
            }
            if (i==pData.size()-1){
                if ("100000000000000".equals(pt.getTime())){
                    canPlayOne = true;
                    KeyValue kv=new KeyValue(imageView.rotateProperty(),Double.parseDouble(pt.getAngle()));
                    KeyFrame kf=new KeyFrame(Duration.millis(1), kv);
                    timeline.getKeyFrames().add(kf);
                    st.getChildren().add(timeline);
                    break;
                }
            }
            Rotation source1 = pData.get(i-1);
            KeyValue kv=new KeyValue(imageView.rotateProperty(),Double.parseDouble(pt.getAngle()));
            KeyFrame kf=new KeyFrame(Duration.millis((Double.parseDouble(pt.getTime())-Double.parseDouble(source1.getTime()))), kv);
            timeline.getKeyFrames().add(kf);
            st.getChildren().add(timeline);
        }
        if (canPlayOne){
            st.setCycleCount(1);
        }else{
            st.setCycleCount(Timeline.INDEFINITE);
        }
        return st;
    }

    public static SequentialTransition initRotationAnimation(Button imageView, List<Rotation> pData){
        SequentialTransition st=new SequentialTransition();
        boolean canPlayOne = false;
        for (int i = 0; i < pData.size(); i++) {
            Timeline timeline=new Timeline();
            Rotation pt = pData.get(i);
            if (i==0){
                KeyValue kv=new KeyValue(imageView.rotateProperty(),Double.parseDouble(pt.getAngle()));
                KeyFrame kf=new KeyFrame(Duration.millis(0), kv);
                timeline.getKeyFrames().add(kf);
                st.getChildren().add(timeline);
                continue;
            }
            if (i==pData.size()-1){
                if ("100000000000000".equals(pt.getTime())){
                    canPlayOne = true;
                    KeyValue kv=new KeyValue(imageView.rotateProperty(),Double.parseDouble(pt.getAngle()));
                    KeyFrame kf=new KeyFrame(Duration.millis(1), kv);
                    timeline.getKeyFrames().add(kf);
                    st.getChildren().add(timeline);
                    break;
                }
            }
            Rotation source1 = pData.get(i-1);
            KeyValue kv=new KeyValue(imageView.rotateProperty(),Double.parseDouble(pt.getAngle()));
            KeyFrame kf=new KeyFrame(Duration.millis((Double.parseDouble(pt.getTime())-Double.parseDouble(source1.getTime()))), kv);
            timeline.getKeyFrames().add(kf);
            st.getChildren().add(timeline);
        }
        if (canPlayOne){
            st.setCycleCount(1);
        }else{
            st.setCycleCount(Timeline.INDEFINITE);
        }
        return st;
    }

    public static SequentialTransition initAlphaAnimation(ImageView imageView, List<Alpha> pData, double rate) {
        SequentialTransition st=new SequentialTransition();
        boolean canPlayOne = false;
        for (int i = 0; i < pData.size(); i++) {
            Alpha pt = pData.get(i);
            if (i==0){
                FadeTransition t=new FadeTransition(Duration.millis(0), imageView);
                t.setToValue(Double.parseDouble(pt.getA())/255);
                st.getChildren().add(t);
                continue;
            }
            Alpha pt1 = pData.get(i-1);
            if (i == pData.size()-1){
                if ("100000000000000".equals(pt.getTime())){
                    canPlayOne = true;
                    FadeTransition t=new FadeTransition(Duration.millis(1), imageView);
                    t.setToValue(Double.parseDouble(pt.getA())/255);
                    st.getChildren().add(t);
                    break;
                }
            }
            FadeTransition t=new FadeTransition(Duration.millis(Double.parseDouble(pt.getTime())-Double.parseDouble(pt1.getTime())), imageView);
            t.setToValue(Double.parseDouble(pt.getA())/255);
            st.getChildren().add(t);
        }
        if (canPlayOne){
            st.setCycleCount(1);
        }else{
            st.setCycleCount(Timeline.INDEFINITE);
        }
        return st;

    }



}
